/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormPencarian.java
 *
 * Created on Nov 30, 2011, 10:25:36 PM
 */
package Pencarian;

import Entity.Buku;
import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author evan_utomo
 */
public class FormPencarian extends javax.swing.JFrame {

    /** Creates new form FormPencarian */
    public FormPencarian() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelSearch2 = new javax.swing.JPanel();
        jTextField2 = new javax.swing.JTextField();
        searchButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        selesaiButton = new javax.swing.JButton();
        panelSearch1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        search1 = new javax.swing.JTextField();
        searchButton1 = new javax.swing.JButton();

        panelSearch2.setBackground(new java.awt.Color(70, 173, 107));

        searchButton2.setText("Search");
        searchButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButton2ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "ID Buku", "Judul Buku", "Pengarang", "Penerbit", "Jumlah", "Stok"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        selesaiButton.setText("Selesai");
        selesaiButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selesaiButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelSearch2Layout = new javax.swing.GroupLayout(panelSearch2);
        panelSearch2.setLayout(panelSearch2Layout);
        panelSearch2Layout.setHorizontalGroup(
            panelSearch2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSearch2Layout.createSequentialGroup()
                .addGap(331, 331, 331)
                .addComponent(searchButton2)
                .addContainerGap(331, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSearch2Layout.createSequentialGroup()
                .addContainerGap(161, Short.MAX_VALUE)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 423, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(143, 143, 143))
            .addGroup(panelSearch2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 707, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSearch2Layout.createSequentialGroup()
                .addContainerGap(576, Short.MAX_VALUE)
                .addComponent(selesaiButton, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        panelSearch2Layout.setVerticalGroup(
            panelSearch2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSearch2Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(searchButton2)
                .addGap(24, 24, 24)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selesaiButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panelSearch1.setBackground(new java.awt.Color(102, 102, 255));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Gambar/membaca-buku.jpg"))); // NOI18N

        searchButton1.setText("Search");
        searchButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelSearch1Layout = new javax.swing.GroupLayout(panelSearch1);
        panelSearch1.setLayout(panelSearch1Layout);
        panelSearch1Layout.setHorizontalGroup(
            panelSearch1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSearch1Layout.createSequentialGroup()
                .addContainerGap(258, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(249, 249, 249))
            .addGroup(panelSearch1Layout.createSequentialGroup()
                .addGap(332, 332, 332)
                .addComponent(searchButton1)
                .addContainerGap(326, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSearch1Layout.createSequentialGroup()
                .addContainerGap(217, Short.MAX_VALUE)
                .addComponent(search1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(204, 204, 204))
        );
        panelSearch1Layout.setVerticalGroup(
            panelSearch1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSearch1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jLabel1)
                .addGap(67, 67, 67)
                .addComponent(search1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addComponent(searchButton1)
                .addContainerGap(75, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 723, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(panelSearch1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 411, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(panelSearch1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void searchButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButton1ActionPerformed
    try {
        this.getContentPane().removeAll();
        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(this.panelSearch2, BorderLayout.CENTER);
        this.getContentPane().setVisible(false);
        this.getContentPane().setVisible(true);

        DefaultTableModel model = (DefaultTableModel) this.jTable1.getModel();

        while (model.getRowCount() != 0) {
            model.removeRow(0);
        }

        Buku[] buku = Buku.search(this.search1.getText());

        for (int i = 0; i < buku.length; i++) {
            Object[] ob = new Object[6];
            ob[0] = buku[i].getIdBuku();
            ob[1] = buku[i].getJudulBuku();
            ob[2] = buku[i].getPengarang();
            ob[3] = buku[i].getPenerbit();
            ob[4] = buku[i].getJumlah();
            ob[5] = buku[i].getStok();
            model.insertRow(i, ob);
        }
    } catch (Exception ex) {
        Logger.getLogger(FormPencarian.class.getName()).log(Level.SEVERE, null, ex);
    }
    search1.setText(null);

}//GEN-LAST:event_searchButton1ActionPerformed

private void searchButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButton2ActionPerformed
    try {
        DefaultTableModel model = (DefaultTableModel) this.jTable1.getModel();

        while (model.getRowCount() != 0) {
            model.removeRow(0);
        }

        Buku[] buku = Buku.search(this.search1.getText());

        for (int i = 0; i < buku.length; i++) {
            Object[] ob = new Object[6];
            ob[0] = buku[i].getIdBuku();
            ob[1] = buku[i].getJudulBuku();
            ob[2] = buku[i].getPengarang();
            ob[3] = buku[i].getPenerbit();
            ob[4] = buku[i].getJumlah();
            ob[5] = buku[i].getStok();
            model.insertRow(i, ob);
        }
    } catch (Exception ex) {
        Logger.getLogger(FormPencarian.class.getName()).log(Level.SEVERE, null, ex);
    }
    jTextField2.setText(null);
}//GEN-LAST:event_searchButton2ActionPerformed

private void selesaiButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selesaiButtonActionPerformed
    this.getContentPane().removeAll();
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(this.panelSearch1, BorderLayout.CENTER);
    this.getContentPane().setVisible(false);
    this.getContentPane().setVisible(true);
    jTextField2.setText(null);
}//GEN-LAST:event_selesaiButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormPencarian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormPencarian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormPencarian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormPencarian.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FormPencarian().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JPanel panelSearch1;
    private javax.swing.JPanel panelSearch2;
    private javax.swing.JTextField search1;
    private javax.swing.JButton searchButton1;
    private javax.swing.JButton searchButton2;
    private javax.swing.JButton selesaiButton;
    // End of variables declaration//GEN-END:variables
}
